package com.biggerlens.commonlistitemview.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.core.content.ContextCompat;

import com.biggerlens.commonlistitemview.R;
import com.biggerlens.commonlistitemview.utils.ScreenUtils;


/**
 * @author chenhong
 * @Date 2021/4/1115:53
 * @package com.darcy.basic.widget
 * @Desciption
 */
public class CommonListItemView extends RelativeLayout {
    ImageView leftImage;
    TextView tvTop;
    TextView tvCenter;
    TextView tvBottom;
    View rightView;
    ImageView ivRight;
    TextView tvRight;
    View twoTittleView;
    View rvItem;
    View divider;

    int leftDrawableWidth=-1;
    int leftDrawableHeight=-1;

    public CommonListItemView(Context context) {
        super(context);
    }

    public CommonListItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public CommonListItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void initView(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.layout_common_list_view, this);
        leftImage = findViewById(R.id.iv_left);
        twoTittleView=findViewById(R.id.rv_tv_two_title);
        tvTop = findViewById(R.id.tv_item_top_title);
        tvBottom = findViewById(R.id.tv_item_bottom_title);
        tvCenter = findViewById(R.id.tv_item_center_title);
        rightView = findViewById(R.id.rv_right);
        ivRight = findViewById(R.id.iv_right_img);
        tvRight = findViewById(R.id.tv_right_text);
        rvItem = findViewById(R.id.rv_item);
        divider=findViewById(R.id.view_divider);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CommonListItemView);


        //左边图标
        Drawable leftDrawable=typedArray.getDrawable(R.styleable.CommonListItemView_liv_icon_left);
        leftDrawableWidth=typedArray.getDimensionPixelSize(R.styleable.CommonListItemView_liv_icon_left_width,-1);
        leftDrawableHeight=typedArray.getDimensionPixelOffset(R.styleable.CommonListItemView_liv_icon_left_height,-1);
        if (leftDrawable!=null){
            setSrcDimens(leftDrawable);
            leftImage.setImageDrawable(leftDrawable);
        }





        //是否显示上下两个标题
        boolean twoTittleVisible=typedArray.getBoolean(R.styleable.CommonListItemView_liv_two_titlle_visible,false);
        twoTittleView.setVisibility(twoTittleVisible?VISIBLE:GONE);

        //标题
        String topText=typedArray.getString(R.styleable.CommonListItemView_liv_tittle_top);
        float topTextSize=typedArray.getDimension(R.styleable.CommonListItemView_liv_tittle_top_size,16.0f);
        int topTextColor=typedArray.getColor(R.styleable.CommonListItemView_liv_tittle_top_color, ContextCompat.getColor(context,R.color.black_8a000000));
        tvTop.setText(topText);
        tvTop.setTextColor(topTextColor);
        //一定要加上这一句可以让字体大小与设置的对应起来
        tvTop.setTextSize(TypedValue.COMPLEX_UNIT_PX,topTextSize);

        String bottomText=typedArray.getString(R.styleable.CommonListItemView_liv_tittle_bottom);
        float bottomTextSize=typedArray.getDimension(R.styleable.CommonListItemView_liv_tittle_bottom_size,12.0f);
        int bottomTextColor=typedArray.getColor(R.styleable.CommonListItemView_liv_tittle_bottom_color, ContextCompat.getColor(context,R.color.black_8a000000));
        tvBottom.setText(bottomText);
        tvBottom.setTextColor(bottomTextColor);
        tvBottom.setTextSize(TypedValue.COMPLEX_UNIT_PX,bottomTextSize);


        //中间文字
        String centerText=typedArray.getString(R.styleable.CommonListItemView_liv_tittle_center);
        float centerTextSize=typedArray.getDimension(R.styleable.CommonListItemView_liv_tittle_center_size, ScreenUtils.sp2px(context,16.0f));
        int centerTextColor=typedArray.getColor(R.styleable.CommonListItemView_liv_tittle_center_color, ContextCompat.getColor(context,R.color.black_de000000));
        tvCenter.setText(centerText);
        tvCenter.setTextColor(centerTextColor);
        tvCenter.setTextSize(TypedValue.COMPLEX_UNIT_PX,centerTextSize);

        //右边图标
        boolean iconRightVisible=typedArray.getBoolean(R.styleable.CommonListItemView_liv_icon_right_visible,false);
        ivRight.setVisibility(iconRightVisible?VISIBLE:GONE);
        Drawable rightImage=typedArray.getDrawable(R.styleable.CommonListItemView_liv_icon_right);
        if (rightImage!=null){
            ivRight.setImageDrawable(rightImage);
        }

        //右边文字
        boolean textRightVisible=typedArray.getBoolean(R.styleable.CommonListItemView_liv_text_right_visible,false);
        tvRight.setVisibility(textRightVisible?VISIBLE:GONE);

        String rightText=typedArray.getString(R.styleable.CommonListItemView_liv_text_right);
        float rightTextSize=typedArray.getDimension(R.styleable.CommonListItemView_liv_text_right_size, ScreenUtils.sp2px(context,12.0f));
        int rightTextColor=typedArray.getColor(R.styleable.CommonListItemView_liv_text_right_color,ContextCompat.getColor(context,R.color.black_8a000000));
        tvRight.setText(rightText);
        tvRight.setTextColor(rightTextColor);
        tvRight.setTextSize(TypedValue.COMPLEX_UNIT_PX,rightTextSize);



        //分隔线
        boolean dividerVisiable=typedArray.getBoolean(R.styleable.CommonListItemView_liv_divider_visiable,false);
        divider.setVisibility(dividerVisiable?VISIBLE:INVISIBLE);

        //资源回收
        typedArray.recycle();

    }


    /**
     * 获得图片
     * @param resId
     * @return
     */
    public Drawable getDrawable(@DrawableRes int resId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            return getResources().getDrawable(resId, null);
        } else {
            return getResources().getDrawable(resId);
        }
    }

    /**
     * 动态改变左边图标大小
     * @param srcDrawable
     */
    public void setSrcDimens(Drawable srcDrawable) {
        if (leftDrawableWidth > 0 && leftDrawableHeight > 0) {
            Bitmap bitmap = Bitmap.createBitmap(leftDrawableWidth, leftDrawableHeight, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            srcDrawable.setBounds(0, 0, leftDrawableWidth, leftDrawableWidth);
            srcDrawable.draw(canvas);
        }
    }


}
